package com.holdtime.jtt809.taxi.module.web.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.holdtime.jtt809.taxi.module.entity.DmsAlarmDO;
import com.holdtime.jtt809.taxi.module.model.vo.DmsAlarmPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.DmsAlarmRespVO;
import com.holdtime.jtt809.taxi.module.model.vo.DmsAlarmSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.DmsAlarmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - dms报警记录")
@RestController
@RequestMapping("/taxi/dms-alarm")
@Validated
public class DmsAlarmController {

    @Resource
    private DmsAlarmService dmsAlarmService;

    @PostMapping("/create")
    @Operation(summary = "创建dms报警记录")
    @PreAuthorize("@ss.hasPermission('taxi:dms-alarm:create')")
    public CommonResult<String> createDmsAlarm(@Valid @RequestBody DmsAlarmSaveReqVO createReqVO) {
        return success(dmsAlarmService.createDmsAlarm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新dms报警记录")
    @PreAuthorize("@ss.hasPermission('taxi:dms-alarm:update')")
    public CommonResult<Boolean> updateDmsAlarm(@Valid @RequestBody DmsAlarmSaveReqVO updateReqVO) {
        dmsAlarmService.updateDmsAlarm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除dms报警记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('taxi:dms-alarm:delete')")
    public CommonResult<Boolean> deleteDmsAlarm(@RequestParam("id") String id) {
        dmsAlarmService.deleteDmsAlarm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得dms报警记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('taxi:dms-alarm:query')")
    public CommonResult<DmsAlarmRespVO> getDmsAlarm(@RequestParam("id") String id) {
        DmsAlarmDO dmsAlarm = dmsAlarmService.getDmsAlarm(id);
        return success(BeanUtils.toBean(dmsAlarm, DmsAlarmRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得dms报警记录分页")
    @PreAuthorize("@ss.hasPermission('taxi:dms-alarm:query')")
    public CommonResult<PageResult<DmsAlarmRespVO>> getDmsAlarmPage(@Valid DmsAlarmPageReqVO pageReqVO) {
        PageResult<DmsAlarmDO> pageResult = dmsAlarmService.getDmsAlarmPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DmsAlarmRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出dms报警记录 Excel")
    @PreAuthorize("@ss.hasPermission('taxi:dms-alarm:export')")
    public void exportDmsAlarmExcel(@Valid DmsAlarmPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DmsAlarmDO> list = dmsAlarmService.getDmsAlarmPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "dms报警记录.xls", "数据", DmsAlarmRespVO.class,
                        BeanUtils.toBean(list, DmsAlarmRespVO.class));
    }

}